import sys
import time
import pytest

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from DButils import select
from DButils import update

from common1.login_operation import *
from common1.bankcard_readdata import get_data
from common1.bankcard_operation import *

class Test_bankcard():

    @pytest.fixture(scope="function", autouse=True)
    def setup_teardown(self):
        service = Service("chromedriver.exe")
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("http://localhost:90")
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        yield
        self.driver.quit()

    #正常添加银行卡
    @pytest.mark.parametrize("username, password, bank, card", get_data(0))
    def test_login(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)
        add_card(self.driver, bank, card)

        sql = "select * from bankcard where userID  = %s and cardBank = %s"
        param = [1, "伊朗中央银行"]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "delete from bankcard where userID  = %s and cardBank = %s"
        param = [1, "伊朗中央银行"]
        update(sql, param)

    #输入17位不正常卡号
    #测试不通过
    @pytest.mark.parametrize("username, password, bank, card", get_data(1))
    def test_abnormal(self, username, password, bank, card):
        sql = "delete from bankcard where userID = %s and cardBank = %s and cardNum = %s"
        param = [1, bank, card]
        update(sql, param)

        login_normal(self.driver, username, password)
        column(self.driver)
        add_card(self.driver, bank, card)

        sql = "select * from bankcard where userID  = %s and cardBank = %s and cardNum = %s"
        param = [1, bank, card]
        expect = len(select(sql, param))
        assert expect == 0

    #重复开卡
    @pytest.mark.parametrize("username, password, bank, card", get_data(2))
    def test_repeat(self, username, password, bank, card):
        sql = "insert into bankcard(cardBank, cardNum, userID) values(%s, %s, %s)"
        param = ["伊拉克国民银行", "1234567891548796", 1]
        update(sql, param)

        login_normal(self.driver, username, password)
        column(self.driver)
        add_card(self.driver, bank, card)

        sql = "select * from bankcard where userID  = %s and cardBank = %s and cardNum = %s"
        param = [1, "伊拉克国民银行", "1234567891548796"]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "delete from bankcard where userID  = %s and cardBank = %s and cardNum = %s"
        param = [1, "伊拉克国民银行", "1234567891548796"]
        update(sql, param)

    #借记卡和信用卡卡号冲突测试
    #测试不通过
    @pytest.mark.parametrize("username, password, bank, card", get_data(3))
    def test_card_type(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)

        self.driver.find_element(By.ID, "bankCard_add_modal_btn").click()
        self.driver.find_element(By.ID, "cardBank_add_input").send_keys(bank)
        self.driver.find_element(By.XPATH,
                                 "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span").click()
        self.driver.find_element(By.ID, "cardNum_add_input").send_keys(card)
        time.sleep(1)
        self.driver.find_element(By.XPATH, "//*[@id='bankCard_save_btn']").click()
        time.sleep(1)

        sql = "select * from bankcard where cardBank = %s and type = %s"
        param = [bank, 2]
        expect = len(select(sql, param))
        assert expect == 0

    #正常删除卡测试
    @pytest.mark.parametrize("username, password, bank, card", get_data(4))
    def test_card_delete(self, username, password, bank, card):
        sql = "select * from bankcard where type = %s"
        param = [1]
        num = len(select(sql, param))

        login_normal(self.driver, username, password)
        column(self.driver)

        self.driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[2]").click()
        time.sleep(1)
        self.driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)

        sql = "select * from bankcard where type = %s"
        param = [1]
        expect = len(select(sql, param))
        assert expect == num - 1

    #删除卡中途退出测试
    @pytest.mark.parametrize("username, password, bank, card", get_data(4))
    def test_delete(self, username, password, bank, card):
        sql = "select * from bankcard where type = %s"
        param = [1]
        num = len(select(sql, param))

        login_normal(self.driver, username, password)
        column(self.driver)

        self.driver.find_element(By.XPATH,
                                 "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[2]").click()
        time.sleep(1)
        self.driver.find_element(By.XPATH,
                                 "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]").click()
        time.sleep(1)

        sql = "select * from bankcard where type = %s"
        param = [1]
        expect = len(select(sql, param))
        assert expect == num

    #修改银行卡名称测试
    # 测试不通过，银行卡名称修改成功
    @pytest.mark.parametrize("username, password, bank, card", get_data(5))
    def test_name(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)

        update_cardBank(self.driver, bank)

        sql = "select * from bankcard where cardBank = %s"
        param = [bank]
        expect = len(select(sql, param))
        assert expect == 0

        # 无法执行后续语句
        sql = "update bankcard SET cardBank = %s where cardBank = %s"
        param = ["中国建设银行", bank]
        update(sql, param)

    #修改银行卡号测试
    @pytest.mark.parametrize("username, password, bank, card", get_data(6))
    def test_num(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)
        update_cardNum(self.driver, card)

        sql = "select * from bankcard where cardNum = %s"
        param = [card]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "update bankcard SET cardNum = %s where cardNum = %s"
        param = ["1234567894521312", card]
        update(sql, param)

    # 修改银行卡号失败测试
    # 测试不通过，非法账号修改成功
    @pytest.mark.parametrize("username, password, bank, card", get_data(7))
    def test_number(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)
        update_cardNum(self.driver, card)

        # sql = "update bankcard SET cardNum = %s where cardNum = %s"
        # param = ["1234567894521312", card]
        # update(sql, param)

        sql = "select * from bankcard where cardNum = %s"
        param = [card]
        expect = len(select(sql, param))
        assert expect == 0

        # 无法执行后续语句
        sql = "update bankcard SET cardNum = %s where cardNum = %s"
        param = ["1234567894521312", card]
        update(sql, param)

    #修改银行卡类别测试
    @pytest.mark.parametrize("username, password, bank, card", get_data(8))
    def test_card_error2(self, username, password, bank, card):
        login_normal(self.driver, username, password)
        column(self.driver)

        sql = "update bankcard SET type = %s where cardBank = %s"
        param = [1, bank]
        update(sql, param)

        self.driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[1]").click()
        self.driver.find_element(By.XPATH, "//*[@id='bankCardUpdateModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span").click()
        self.driver.find_element(By.ID, "bankCard_update_btn").click()
        time.sleep(1)

        sql = "select * from bankcard where cardBank = %s and type = %s"
        param = [bank, 2]
        expect = len(select(sql, param))
        print(select(sql, param))
        assert expect == 1


















